﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;
using VOL;
namespace Shopper
{
    public partial class frmLoaiHangManager : DevExpress.XtraEditors.XtraForm
    {
        LoaiHangBL loaihangbl = new LoaiHangBL();
        public frmLoaiHangManager()
        {
            InitializeComponent();
        }

        private void frmLoaiHangManager_Load(object sender, EventArgs e)
        {
            FillData();
        }

        private void FillData()
        {
            gridControl1.DataSource = loaihangbl.GetData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            LoaiHang lh = new LoaiHang();

            using (frmLoaiHangForm frm = new frmLoaiHangForm())
            {
                if (frm.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    lh.TenLoaiHang = frm.TenLoaiHang;

                    loaihangbl.insertLoaiHang(lh);
                    XtraMessageBox.Show("Đã thêm loại hàng hóa thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FillData();
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult dr = XtraMessageBox.Show("Bạn có muốn xóa bản ghi này hay k?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == System.Windows.Forms.DialogResult.Yes)
            {
                var row = gridView1.FocusedRowHandle;
                var obj = gridView1.GetRowCellValue(row, "MaLoaiHang");

                LoaiHang lh = new LoaiHang();
                lh.MaLoaiHang = (int)obj;
                loaihangbl.deleteLoaiHang(lh);
                FillData();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            using (frmLoaiHangForm frm = new frmLoaiHangForm())
            {
                int MaLoaiHang = (int)gridView1.GetFocusedRowCellValue("MaLoaiHang");
                LoaiHang objLoaiHang = new LoaiHang();
                objLoaiHang.MaLoaiHang = MaLoaiHang;

                DataTable dtMDHang = loaihangbl.searchIDLoaiHang(objLoaiHang);
                if (dtMDHang.Rows.Count > 0)
                {
                    frm.TenLoaiHang = dtMDHang.Rows[0]["TenLoaiHang"].ToString();
                }

                if (frm.ShowDialog(this.FindForm()) == System.Windows.Forms.DialogResult.OK)
                {
                    LoaiHang objLoaiHangNew = new LoaiHang();
                    objLoaiHangNew.TenLoaiHang = frm.TenLoaiHang;
                    objLoaiHangNew.MaLoaiHang = MaLoaiHang;

                    loaihangbl.updateLoaiHang(objLoaiHangNew);

                    XtraMessageBox.Show("Đã thay đổi loại hàng hóa thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FillData();
                }
            }
        }
    }
}